/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.client.solrj.response;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.solr.SolrJettyTestBase;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.apache.HttpSolrClient;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.request.SolrQuery;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/** A test for parsing Solr response from query by InputStreamResponseParser. */
public class InputStreamResponseParserTest extends SolrJettyTestBase {

  private static InputStream getResponse() {
    return new ByteArrayInputStream("NO-OP test response".getBytes(StandardCharsets.UTF_8));
  }

  @BeforeClass
  public static void beforeTest() throws Exception {
    createAndStartJetty(legacyExampleCollection1SolrHome());
  }

  @Before
  public void doBefore() throws IOException, SolrServerException {
    // add document and commit, and ensure it's there
    SolrClient client = getSolrClient();
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", "1234");
    client.add(doc);
    client.commit();
  }

  /** Parse response from query using InputStreamResponseParser. */
  @Test
  public void testQueryParse() throws Exception {

    try (SolrClient client =
        new HttpSolrClient.Builder(getBaseUrl())
            .withDefaultCollection(DEFAULT_TEST_CORENAME)
            .withResponseParser(new InputStreamResponseParser("xml"))
            .build()) {
      SolrQuery query = new SolrQuery("id:1234");
      QueryRequest req = new QueryRequest(query);
      NamedList<Object> resp = client.request(req);
      String responseString = InputStreamResponseParser.consumeResponseToString(resp);
      assertResponse(responseString);
    }
  }

  private void assertResponse(String responseString) throws IOException {
    ResponseParser xmlResponseParser = new XMLResponseParser();
    NamedList<?> expectedResponse =
        xmlResponseParser.processResponse(
            new ByteArrayInputStream(responseString.getBytes(StandardCharsets.UTF_8)), "UTF-8");
    @SuppressWarnings({"unchecked"})
    List<SolrDocument> documentList =
        (List<SolrDocument>) expectedResponse.getAll("response").get(0);
    assertEquals(1, documentList.size());
    SolrDocument solrDocument = documentList.get(0);
    assertEquals("1234", String.valueOf(solrDocument.getFieldValue("id")));
  }

  /** Parse response from java.io.InputStream. */
  @Test
  public void testInputStreamResponse() throws Exception {
    InputStreamResponseParser parser = new InputStreamResponseParser("xml");
    try (final InputStream is = getResponse()) {
      assertNotNull(is);
      assertThrows(
          UnsupportedOperationException.class,
          () -> {
            parser.processResponse(is, "UTF-8");
          });
    }
  }
}
